home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-18 | 3.8 KB | 142 lines | [TEXT/R*ch] |
- // (c) copyright 1995,1996, Jon Kalb, Liberty Software
- // Kalb@LibertySoft.com
- // You may freely incorporate this code into any machine-
- // readable project. You may modify this code, but you may
- // not remove this statement.
-
- #ifndef _DEBUGBUF_
- #define _DEBUGBUF_
-
- #include <Types.h> // for "true" and "false"
-
- class debugbuf: public streambuf
- {
- public:
-
- //debugbuf();
- //virtual ~debugbuf();
- // Construction and destruction are really handled
- // by class debugbufinit. Initialization is done
- // in the private routine init().
-
- protected:
-
- // These protected member functions are virtual
- // functions in the parent (streambuf) class that
- // we override to create our behavior.
-
- // The name overflow may be confusing--this
- // just outputs a character to the stream.
- // Calls outputchar().
- virtual int overflow(int c = EOF);
-
- // Since this is not an input stream, we want both
- // pbackfail() and underflow() to return EOF (thus
- // indicating failure). It turns out that the
- // default behavior (the base class implementation)
- // does just that.
- //virtual int pbackfail(int c = EOF);
- //virtual int underflow();
-
- // we don't do input so always return EOF
- virtual int uflow() {return EOF;}
-
- // we don't do input so always return 0 chars read
- virtual int xsgetn(char *, int) {return 0;}
-
- // Calls outputchar() for each character.
- virtual int xsputn(const char *s, int n);
-
- // We use the default behavior which returns
- // a streampos that is in an invalid position.
- // We do not support repositioning on this
- // stream.
- //virtual streampos seekoff(streamoff off,
- // ios::seekdir way,
- // ios::openmode which =
- // ios::in | ios::out);
- //virtual streampos seekpos(streampos sp,
- // ios::openmode which =
- // ios::in | ios::out);
-
- // We don't support setting the buffer so
- // we use the default which is just to return
- // "this."
- //virtual streambuf *setbuf(char *s, int n);
-
- // There is nothing to sync with, so we
- // just do the default which is to return
- // zero, indicating no error.
- //virtual int sync();
- // Actually, as an alternative implementation it
- // would be possible to use this function to call
- // our soft flush routine. In practice, there would
- // be no different result. sync() is usually only
- // called by pubsync(), which is usually only called
- // by flush(), which is usually only called by
- // the endl manipulator function after it has
- // streamed '\n'. So the soft flush would always
- // follow a hard flush and result in a no-op.
-
- private:
- enum
- {
- kMaxDebugStrReadableString = 60,
- kSizeOfSemicolonG = 2,
- kSizeOfLengthByte = 1,
- kTabSize = 5,
- kStop = true
- };
-
- // the buffer
- char pbeg[ kSizeOfLengthByte +
- kMaxDebugStrReadableString +
- kSizeOfSemicolonG];
-
- // location of the next streamed char
- char *pnext;
-
- // This always points to the end of the readable
- // string buffer. Once it is set in init(), it is
- // never modified
- char *pend;
-
- // the number of queued alerts
- int alertCount;
-
- void init();
-
- void outputchar(char c);
- void formfeed();
- void horztab();
- void backspace();
- void verttab();
- void alert();
- void addchar(char c);
- void softflush();
- void flushalerts();
- void flushdebugstring(int stop = false);
-
- friend class debugbufinit;
- };
-
- class debugbufinit
- {
- static unsigned int count;
- public:
- debugbufinit();
-
- // Our destructor is not virtual. It is important
- // that objects of this class have no memory
- // footprint. We will end up with one object of this
- // class per translation unit (.cp file). If this
- // class has any virtual member functions then
- // objects of this class would have v tables in
- // memory. Since this is not intended to be a base
- // class for other class, there is no need to be
- // virtual.
- ~debugbufinit();
- };
-
- #endif // _DEBUGBUF_
-